/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.BorrowerDAO;
import com.cashmate.hibernate.dao.CollectionsDAO;
import com.cashmate.hibernate.dao.DocumentDAO;
import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.dao.PaymentsDAO;
import com.cashmate.hibernate.pojo.Areas;
import com.cashmate.hibernate.pojo.Borrowers;
import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Collections;
import com.cashmate.hibernate.pojo.Deposits;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Loans;
import com.cashmate.hibernate.pojo.Payments;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Dine
 */
public class GetSeq extends ActionSupport {

    //private static Logger logger = Logger.getLogger(GetAreas.class);
    private CollectionsDAO colldao = (CollectionsDAO) ServletActionContext.getServletContext().getAttribute("collectionsDAO");
    private PaymentsDAO paydao = (PaymentsDAO) ServletActionContext.getServletContext().getAttribute("paymentsDAO");
    private DocumentDAO docdao = (DocumentDAO) ServletActionContext.getServletContext().getAttribute("documentDAO");
    private EmployeeDAO empdao = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private LoansDAO loandao = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private BorrowerDAO bordao = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private Collections collseq;
    private List<Deposits> deposit;
    private List<Payments> payment;
    private Documents document;
    private int sampleseq;
    private Employees employee;
    private Areas area;
    private List<Loans> loans;
    private List<Borrowers> borrower;
    private String loc;
    private String actstatus;
    private String statprevious;

    public String getStatprevious() {
        return statprevious;
    }

    public void setStatprevious(String statprevious) {
        this.statprevious = statprevious;
    }

    public String getLoc() {
        return loc;
    }

    public String getActstatus() {
        return actstatus;
    }

    public void setActstatus(String actstatus) {
        this.actstatus = actstatus;
    }

    public void setSampleseq(int sampleseq) {
        this.sampleseq = sampleseq;
    }

    public void setBorrower(List<Borrowers> borrower) {
        this.borrower = borrower;
    }

    public void setCollseq(Collections collseq) {
        this.collseq = collseq;
    }

    public void setDeposit(List<Deposits> deposit) {
        this.deposit = deposit;
    }

    public void setDocument(Documents document) {
        this.document = document;
    }

    public void setEmployee(Employees employee) {
        this.employee = employee;
    }

    public void setLoans(List<Loans> loans) {
        this.loans = loans;
    }

    public void setPayment(List<Payments> payment) {
        this.payment = payment;
    }

    public List<Deposits> getDeposit() {
        return deposit;
    }

    public List<Payments> getPayment() {
        return payment;
    }

    public Documents getDocument() {
        return document;
    }

    public Employees getEmployee() {
        return employee;
    }

    public List<Loans> getLoans() {
        return loans;
    }

    public List<Borrowers> getBorrower() {
        return borrower;
    }

    public Collections getCollseq() {
        return collseq;
    }

    public Areas getArea() {
        return area;
    }

    public void setArea(Areas area) {
        this.area = area;
    }

    @Action(value = "/get-seq", results = {
        @Result(name = "input", location = "${loc}"),
        @Result(name = "success", location = "${loc}")
    })
    @Override
    public String execute() {
        try {
            collseq = colldao.get(sampleseq);
            Branches b = (Branches) ServletActionContext.getRequest().getSession().getAttribute("branch");


            if (collseq.getBranch().equals(b.getBranchname()) || (b.getBranchname().equals("Head Office"))) {
                loans = new ArrayList<Loans>();
                borrower = new ArrayList<Borrowers>();
                payment = new ArrayList<Payments>();

                List<Payments> pym = paydao.getByCollectionID(collseq);

                for (int i = 0; i < pym.size(); i++) {
                    payment.add(pym.get(i));
                    loans.add(i, loandao.get(pym.get(i).getLoans().getLoanid()));
                    borrower.add(i, bordao.get(loans.get(i).getBorrowers().getBorrowerid()));
                }

                area = collseq.getAreas();
                document = docdao.get(collseq.getDocuments().getDocumentid());
                employee = empdao.get(collseq.getAreas().getEmployees().getEmployeeid());
                ServletActionContext.getRequest().setAttribute("collection", collseq);
                ServletActionContext.getRequest().setAttribute("paylist", payment);
                ServletActionContext.getRequest().setAttribute("document", document);
                ServletActionContext.getRequest().setAttribute("employee", employee);
                ServletActionContext.getRequest().setAttribute("borrower", borrower);
                ServletActionContext.getRequest().setAttribute("area", area);
                statprevious = "yes";

                if (collseq.getStat().equals("saved")) {
                    System.out.println("napunta sa saved");
                    loc = "../../loancollection.jsp?cat=savedcoll";
                } else if (collseq.getStat().equals("posted")) {
                    System.out.println("napunta sa saved");
                    loc = "../../loancollection.jsp?cat=history";
                }
            } else {
                actstatus = "You are not allowed to view the collection of other branch.";
                loc = "../../loancollection.jsp?cat=coll";
            }


            return SUCCESS;

        } catch (NullPointerException e) {
            actstatus = "Sequence Number does not exist.";
            loc = "../../loancollection.jsp?cat=coll";

        } catch (Exception e) {
            actstatus = e.toString();
            loc = "../../loancollection.jsp?cat=coll";
        }

        return SUCCESS;
    }
}
